package com.pan.api.format.impl.markdown;


import com.pan.api.entity.ControllerInformation;
import com.pan.api.entity.MethodInformation;
import com.pan.api.format.ControllerFormat;
import com.pan.api.format.MethodFormat;

import java.util.Comparator;
import java.util.List;

/**
 * String docTitle = "PānQíĀn Generate interfaces documentation";
 * String controllerTemplate;{
 * controllerTemplate = """
 * ## {controllerName}
 * comment: {comment}
 * prefix: {prefix}
 * method:
 * {methodList}
 * -------------------------------
 * """;
 * }
 * String methodTemplate;{
 * methodTemplate = """
 * ### {methodName}
 * comment: {comment}
 * url: {url}
 * request type: {requestType}
 * argument:
 * {argList}
 * """;
 * }
 * String argumentTemplate;{
 * argumentTemplate = """
 * #### {parameterName}
 * comment: {comment}
 * request type: {requestType}
 * argument form: {argumentForm}
 * <br>
 * """;
 * }
 */

/**
 * 坑爹，貌似java的多行字符串会帮忙把结尾的空格给清洗掉，导致markdown的格式出现问题
 */
public final class MarkdownControllerFormat extends ControllerFormat {
    public MarkdownControllerFormat(MethodFormat methodStandardization) {
        super(methodStandardization);
    }
    {//controllerName,comment,isJson,prefix,methodListStr
        String template =
                """
                ## Controller: {controllerName}
                - comment: {comment}
                - isJson: {isJson}
                - prefix: {prefix}
                - methods:
                  {methodListStr}   
                ---
                """;
        String regex = "\\{\\w+?\\}";
        initialization(template, regex);
    }
    @Override
    public String convert(ControllerInformation controllerInformation) {
        String controllerName = controllerInformation.getControllerName();
        String comment = controllerInformation.getComment();
        String isJson =  controllerInformation.isJsonXml()?"true":"false";
        String prefix = controllerInformation.getPrefix();
        List<MethodInformation> methodList = controllerInformation.getMethodList()
                .stream().sorted(Comparator.comparing(methodInformation->methodInformation.getMethodName())).toList();
        String methodListStr = "";
        if (methodList!=null) {
            methodListStr = methodList.stream().map(methodInformation ->  super.methodFormat.convert(methodInformation))
                    .reduce((previous,current)->previous.concat(current)).get();
        }
        return replace(controllerName,comment,isJson,prefix,methodListStr);

    }
}
